/*
 * @Description: 公共配置项（表单、表格等）
 * @Author: HuiSir<https://gitee.com/huisir001>
 * @Date: 2025-06-23 22:57:36
 * @LastEditTime: 2025-09-19 12:02:28
 */

import { ICol, IFormOption } from "@/ui";
import { project } from "@/init/config";
import { useGetters, useAdminStore } from "@/store";
import { RichEditorUploadImageConfig } from "@/ui";
import { basename } from "@/ui/helpers/path";

const apiBaseUrl = import.meta.env.VITE_BASE_API;
const fileGetPath = import.meta.env.VITE_FILE_GET_PATH;

export const tableColsMixin: ICol[] = [
  {
    label: "创建人",
    minWidth: 80,
    prop: "createUser",
    showOverflowTooltip: true,
  },
  {
    label: "更新人",
    minWidth: 80,
    prop: "updateUser",
    showOverflowTooltip: true,
  },
  {
    label: "创建时间",
    minWidth: 130,
    sortable: true,
    prop: "createTime",
  },
  {
    label: "更新时间",
    minWidth: 130,
    sortable: true,
    prop: "updateTime",
  },
];

export const tableColsTimeMixin: ICol[] = [
  {
    label: "创建时间",
    minWidth: 130,
    sortable: true,
    prop: "createTime",
  },
  {
    label: "更新时间",
    minWidth: 130,
    sortable: true,
    prop: "updateTime",
  },
];

/**
 * 表单上传组件配置
 * 这里默认限制图片，可更改
 */
export const useFormUploadProp = (customOpt?: IFormOption["props"]) => {
  const Opt: IFormOption["props"] = {
    fieldType: "string",
    maxFileSize: 2 * 1024 * 1024,
    action: apiBaseUrl + "/file/upload",
    headers: { [project.token.key]: useGetters().getToken },
    name: "file",
    multiple: false,
    listType: "text",
    autoUpload: false, // 默认不自动上传
    // 这里文件类型给个默认
    data: {
      fileType: basename(location.pathname),
    },
    callback(res) {
      const code = res[project.responseFields.code];
      if (code) {
        if (code === project.responseBaseCode.success) {
          return (fileGetPath || "") + res?.data;
        } else if (code === project.responseBaseCode.loginInvalid) {
          useAdminStore().LoginInvalid();
          return "";
        }
      }
      return "";
    },
  };

  Object.assign(Opt, customOpt || {});

  return Opt;
};

/**
 * 富文本编辑图片上传配置项
 */
export const richEditorUploadImageConfigMixin: RichEditorUploadImageConfig = {
  server: apiBaseUrl + "/file/upload",
  headers: {
    [project.token.key]: useGetters().getToken,
  },
  callback: (res) => {
    const code = res[project.responseFields.code];
    if (code) {
      if (code === project.responseBaseCode.success) {
        return (fileGetPath || "") + res.data;
      } else if (code === project.responseBaseCode.loginInvalid) {
        useAdminStore().LoginInvalid();
        return "";
      }
    }
    return "";
  },
};
